package br.senai.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import br.senai.entity.Usuario;
import br.senai.service.LoginService;


@WebServlet("/login")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
  
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String login = request.getParameter("login");
		String senha = request.getParameter("senha");
		
		LoginService ls = new LoginService();
		Boolean usuarioLogado = ls.validaLogin(login, senha);
		
		Usuario usuario = new Usuario();
		usuario.setLogin(login);
		usuario.setSenha(senha);
		
		HttpSession session = request.getSession();
		session.setAttribute("usuario", usuario);
		RequestDispatcher rd;
		if(usuarioLogado){
			rd = request.getRequestDispatcher("home.jsp");
		} else {
			rd = request.getRequestDispatcher("index.jsp");

		}
        rd.forward(request, response);
		
	
	}

}
